﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using BitPortal.Models;
using BitPortal.Business;
using BitPortal.Utils;
using System.Web.Security;

namespace BitPortal.Modules.UserInfo
{
    public partial class UserInfo : Controls.BaseUserControl
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        protected override void OnInit(EventArgs e)
        {
            PermissionBLL.CheckLogin();
            (this.Page as Controls.BaseFrontendPage).IncludeCssFile("/jquery-ui.custom/css/smoothness/jquery-ui-1.8.16.custom.css");
            (this.Page as Controls.BaseFrontendPage).IncludeScriptFile("/jquery-ui.custom/js/jquery-ui-1.8.16.custom.min.js");
            base.OnInit(e);
            UploadFile1.MaxFileLength = 200;
            UploadFile1.AllowExtentions = "jpg,jpeg,png,gif";
            UploadFile1.FileUpload.MaxFiles = 1;
            UploadFile1.AllowTypes = "";
            BindData();
        }
        protected void BindData()
        {
            MembershipUser user = Membership.GetUser();
            Models.UserInfo uinfo = UserBLL.GetUserInfo(user);
            if (uinfo != null)
            {
                lblUserName.Text = user.UserName;
                lblEmail.Text = user.Email;
                lblFirstName.Text = hFirstName.Value = (string.IsNullOrEmpty(uinfo.FirstName)) ? "NAN" : uinfo.FirstName;
                lblLastName.Text = hLastName.Value = (string.IsNullOrEmpty(uinfo.LastName)) ? "NAN" : uinfo.LastName;
                lblCity.Text = hCity.Value = (string.IsNullOrEmpty(uinfo.City)) ? "NAN" : uinfo.City;
                lblAddress.Text = hAddress.Value = (string.IsNullOrEmpty(uinfo.Address)) ? "NAN" : uinfo.Address;
                lblBirth.Text = hBirth.Value = (uinfo.Bithday != null) ? ((DateTime)uinfo.Bithday).ToString("dd/MM/yyyy") : "";
                lblGender.Text = hGender.Value = (uinfo.Gender == true) ? Resources.BitResources.Male : Resources.BitResources.Female;
                lblIdentity.Text = hIdentity.Value = (string.IsNullOrEmpty(uinfo.IdentityNumber)) ? "NAN" : uinfo.IdentityNumber;
                imagepreview.Src = uinfo.Avarta;
            }
        }
        protected void imgOk_Click(object sender, EventArgs e)
        {
            ExtendedBitPortalEntities bitEnt = new ExtendedBitPortalEntities();
            var id = new Guid(Membership.GetUser().ProviderUserKey.ToString());
            Models.UserInfo uinfo = bitEnt.UserInfos.SingleOrDefault(ent => ent.CustomerID.Equals(id));
            if (uinfo != null)
            {
                uinfo.FirstName = hFirstName.Value;
                uinfo.LastName = hLastName.Value;
                uinfo.Bithday = hBirth.Value.ToDateTimeTryParse();
                uinfo.City = hCity.Value;
                uinfo.Address = hAddress.Value;
                uinfo.Gender = hGender.Value.Trim().Equals(Resources.BitResources.Male);
                uinfo.IdentityNumber = hIdentity.Value;
                if (UploadFile1.UploadedFiles != null)
                {
                    uinfo.Avarta = UploadFile1.UploadedFiles[0].FileName_SavedAs.Substring(1);
                }
            }
            if (bitEnt.SaveChanges() > 0)
            {
                (this.Page as Controls.BaseFrontendPage).ShowArlert("Cập nhật thông tin thành công!");
                RegisterScript("LoadImage('"+imagepreview.ClientID+"','" + uinfo.Avarta + "')");
            }
        }
    }
}